package com.adapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.adapter.VideoListAdapter.GetCSDNLogoTask;
import com.adapter.VideoListAdapter.ViewHolder;
import com.entity.FileInfo;
import com.entity.Navigation;
import com.videoplayer.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class DownloadEditAdapter extends BaseAdapter {

	private List<FileInfo> list = new ArrayList<FileInfo>();
	private ViewHolder viewHolder;
	private Context c;
	private String flag;
	private java.text.DecimalFormat df;
	private boolean[] itemStatus;
	
		
		

	public DownloadEditAdapter(Context c, List<FileInfo> list) {
		this.c = c;
		this.list = list;
		itemStatus = new boolean[list.size()];
		for(int i =0;i<list.size();i++){
			itemStatus[i] = false;
		}
		df = new java.text.DecimalFormat("#.##");
	}
	
	public void toggle(int position){
		if(itemStatus[position] == true){
			itemStatus[position] = false;
		}else{
			itemStatus[position] = true;
		}
		this.notifyDataSetChanged();//date changed and we should refresh the view
	}
	
	public int[] getSelectedItemIndexes() {

		if (itemStatus == null || itemStatus.length == 0) {
			return new int[0];
		} else {
			int size = itemStatus.length;
			int counter = 0;
			// TODO how can we skip this iteration?
			for (int i = 0; i < size; i++) {
				if (itemStatus[i] == true)
					++counter;
			}
			int[] selectedIndexes = new int[counter];
			int index = 0;
			for (int i = 0; i < size; i++) {
				if (itemStatus[i] == true)
					selectedIndexes[index++] = i;
			}
			return selectedIndexes;
		}
	};
	
	public void resetAllCheck(){
		int size = itemStatus.length;
		for (int i = 0; i < size; i++) {
			itemStatus[i]=false;
		}
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public FileInfo getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setFlag(String i) {
		flag = i;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {

		if (convertView == null) {
			convertView = View.inflate(c, R.layout.down_listview_edit_item, null);
			viewHolder = new ViewHolder();
			viewHolder.title = (TextView) convertView
					.findViewById(R.id.tv_down_edit_title);
			viewHolder.size = (TextView) convertView
					.findViewById(R.id.tv_down_edit_size);
			viewHolder.time = (TextView) convertView
					.findViewById(R.id.tv_down_edit_time);
			viewHolder.ivSing = (ImageView) convertView
					.findViewById(R.id.iv_edit_down);
			viewHolder.checkBox = (ImageView) convertView
					.findViewById(R.id.cb_check);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
//		viewHolder.checkBox
//		.setOnCheckedChangeListener(new MyCheckBoxChangedListener(
//				arg0));
		if (list != null) {
			long time = list.get(arg0).motifyTime;
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(time);
			viewHolder.title.setText(list.get(arg0).Name);

			viewHolder.size.setText(df.format(list.get(arg0).Size) + "Mb");
			viewHolder.time.setText(cal.getTime().toGMTString());
			if (itemStatus[arg0] == true) {
				viewHolder.checkBox.setBackgroundResource(R.drawable.focused);
				viewHolder.ivSing.setBackgroundResource(R.drawable.item_delete);
			} else {
				viewHolder.checkBox.setBackgroundResource(R.drawable.normal);
				viewHolder.ivSing.setBackgroundResource(R.drawable.item_download_complete);
			}
		}
		return convertView;
	}
	
	class MyCheckBoxChangedListener implements OnCheckedChangeListener {
		int position;

		MyCheckBoxChangedListener(int position) {
			this.position = position;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			System.out.println("" + position + "Checked?:" + isChecked);
			if (isChecked){
//				itemStatus[position] = true;
//				DownloadEditAdapter.this.notifyDataSetChanged();
			}
			else{
//				itemStatus[position] = false;
//				DownloadEditAdapter.this.notifyDataSetChanged();
			}
		}

	}

	static class ViewHolder {
		private TextView title;
		private TextView size;
		private TextView time;
		private ImageView ivSing;
		private ImageView checkBox;
	}

}
